﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waiter.Controler;
using Waiter.Common;

namespace Waiter.View
{
    public partial class FrmTables : DevComponents.DotNetBar.Office2007Form
    {
        public FrmTables()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmTables.btnClose_Click", "", "");
            this.Close();
        }

        bool loadingFlag = CommonConstants.FLAG_OFF;
        private void FrmTables_Load(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmTables.FrmTables_Load", "", "");
            loadingFlag = CommonConstants.FLAG_ON;
            fsLoadCmbArea();
            fsLoadAreaInfo();
            fsLoadTableList();
            loadingFlag = CommonConstants.FLAG_OFF;
        }

        public void fsLoadCmbArea()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmTables.fsLoadCmbArea", "", "");
            MasterCtrl masterCtrl = new MasterCtrl();
            masterCtrl.fsLoadCmbGroupList(this.cmbArea, CommonConstants.CAC_KHU_VUC, null);
        }

        public void fsLoadAreaInfo()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmTables.fsLoadAreaInfo", "", "");
            string selectedArea = CommonCtrl.fsCmbGetSelectedValue(this.cmbArea);
            MasterCtrl masterCtrl = new MasterCtrl();

            // load area sign
            MASTER areaSign = masterCtrl.fsGetMasterFromGroupCode(CommonConstants.MA_KHU_VUC, selectedArea);
            if (areaSign != null)
            {
                this.txtAreaSign.Text = areaSign.VALUE;
            }
            else
            {
                this.txtAreaSign.Text = CommonUtils.fsGetFirstChars(CommonCtrl.fsCmbGetSelectedText(this.cmbArea));
            }

            // load area price
            MASTER areaPrice = masterCtrl.fsGetMasterFromGroupCode(CommonConstants.GIA_KHU_VUC, selectedArea);
            if (areaPrice != null)
            {
                int price = Convert.ToInt32(areaPrice.VALUE);
                this.txtPrice.Value = price;
            }
            else
            {
                this.txtPrice.Text = "0";
            }
        }

        public void fsLoadTableList()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmTables.fsLoadTableList", "", "");
            string selectedArea = CommonCtrl.fsCmbGetSelectedValue(this.cmbArea);
            TableCtrl tableCtrl = new TableCtrl();
            List<TABLE> tableList = tableCtrl.fsGetAllTableList(selectedArea);
            this.dgrTable.Rows.Clear();
            if (tableList == null || tableList.Count == 0)
            {
                this.txtTableCount.Value = 0;
                return;
            }

            this.txtTableCount.Value = tableList.Count;
            this.dgrTable.Rows.Add(tableList.Count);
            for (int i = 0; i < tableList.Count; i++)
            {
                this.dgrTable.Rows[i].Cells["colSTT"].Value = (i + 1).ToString("00");
                this.dgrTable.Rows[i].Cells["colTableCode"].Value = tableList[i].CODE;
                this.dgrTable.Rows[i].Cells["colTableName"].Value = tableList[i].TABLE_NAME;
                this.dgrTable.Rows[i].Cells["colStatusCode"].Value = tableList[i].STATUS_CODE;
                this.dgrTable.Rows[i].Cells["colStatus"].Value = this.fsGetStatusName(tableList[i].STATUS_CODE);

                if (tableList[i].STATUS_CODE == (int)TableStatus.BUSY
                    || tableList[i].STATUS_CODE == (int)TableStatus.MERGE)
                {
                    this.dgrTable.Rows[i].DefaultCellStyle.BackColor = Color.LightGray;
                }
            }
        }

        private string fsGetStatusName(int code)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmTables.fsGetStatusName", "", "");
            string resultStr = "";
            switch (code)
            {
                case (int)TableStatus.BUSY:
                    resultStr = "Đang hoạt động";
                    break;
                case (int)TableStatus.EMPTY:
                    resultStr = "Bàn trống";
                    break;
                case (int)TableStatus.MERGE:
                    resultStr = "Đang hoạt động";
                    break;
                case (int)TableStatus.NOT_USE:
                    resultStr = "Không sử dụng";
                    break;
            }
            return resultStr;
        }

        private void cmbArea_SelectedIndexChanged(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmTables.cmbArea_SelectedIndexChanged", "", "");
            fsLoadAreaInfo();
            fsLoadTableList();
        }

        private void txtPrice_ValueChanged(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmTables.txtPrice_ValueChanged", "", "");
            if (this.txtPrice.Value == 0)
            {
                this.lblPriceType.Text = "";
            }
            else if (this.txtPrice.Value > 0)
            {
                this.lblPriceType.Text = "Tăng";
            }
            else
            {
                this.lblPriceType.Text = "Giảm";
            }
        }

        private void btnAddArea_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmTables.btnAddArea_Click", "", "");
            frmGroupList frm = new frmGroupList(CommonConstants.CAC_KHU_VUC);
            frm.ShowDialog();
            this.FrmTables_Load(null, null);
        }

        private void btnRefreshArea_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmTables.btnRefreshArea_Click", "", "");
            this.FrmTables_Load(null, null);
        }

        private void btnSaveArea_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmTables.btnSaveArea_Click", "", "");
            MasterCtrl masterCtrl = new MasterCtrl();
            ResultStatus rs = masterCtrl.fsInsertOrUpdateSingle(CommonConstants.GIA_KHU_VUC,
                "Gia khu vuc",
                CommonCtrl.fsCmbGetSelectedValue(this.cmbArea),
                this.txtPrice.Text);
            if (rs == ResultStatus.ERROR)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Đã có lổi xảy ra, vui lòng thực hiện lại!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            rs = masterCtrl.fsInsertOrUpdateSingle(CommonConstants.MA_KHU_VUC,
                 "Ma khu vuc",
                 CommonCtrl.fsCmbGetSelectedValue(this.cmbArea),
                 this.txtAreaSign.Text);
            if (rs == ResultStatus.ERROR)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Đã có lổi xảy ra, vui lòng thực hiện lại!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (this.dgrTable.Rows.Count > 0)
            {
                TableCtrl tableCtrl = new TableCtrl();
                DialogResult dlgRs = DevComponents.DotNetBar.MessageBoxEx.Show("Bạn có muốn thay đổi tên bàn theo mã khu vực mới không?", CommonMessages.MSG_CONFIRM_TITLE, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dlgRs == System.Windows.Forms.DialogResult.Yes)
                {
                    for (int i = 0; i < this.dgrTable.Rows.Count; i++)
                    {
                        this.dgrTable.Rows[i].Cells["colTableName"].Value =
                            this.txtAreaSign.Text + this.dgrTable.Rows[i].Cells["colSTT"].Value.ToString();
                        tableCtrl.fsUpdateTableName(this.dgrTable.Rows[i].Cells["colTableCode"].Value.ToString(),
                            this.dgrTable.Rows[i].Cells["colTableName"].Value.ToString());
                    }
                }
            }
            DevComponents.DotNetBar.MessageBoxEx.Show("Đã cập nhật thành công!", CommonMessages.MSG_NOTICE_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmTables.btnAdd_Click", "", "");
            if (!fsAddTable())
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Đã có lổi xảy ra, vui lòng thực hiện lại!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            this.txtTableCount.Text = this.dgrTable.Rows.Count.ToString();
        }

        public bool fsAddTable()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmTables.fsAddTable", "", "");
            TableCtrl tableCtrl = new TableCtrl();
            TABLE item = new TABLE();
            item.CODE = CommonUtils.fsCreateNewCode();
            item.AREA_CODE = CommonCtrl.fsCmbGetSelectedValue(this.cmbArea);
            item.TABLE_NAME = tableCtrl.fsGetNewTableName(
                CommonCtrl.fsCmbGetSelectedValue(this.cmbArea),
                this.txtAreaSign.Text);
            item.STATUS_CODE = (int)TableStatus.EMPTY;
            item.OPID = CommonUtils.fsGetOPID();
            item.DEL_FLG = CommonConstants.FLAG_OFF;
            item.ADD_DATE = DateTime.Now;
            item.UPD_DATE = DateTime.Now;

            if (tableCtrl.fsInsertTable(item) != ResultStatus.SUCCESS)
            {
                return false;
            }

            this.dgrTable.Rows.Add(1);
            int idx = this.dgrTable.Rows.Count - 1;
            this.dgrTable.Rows[idx].Cells["colSTT"].Value = (idx + 1).ToString("00");
            this.dgrTable.Rows[idx].Cells["colTableCode"].Value = item.CODE;
            this.dgrTable.Rows[idx].Cells["colTableName"].Value = item.TABLE_NAME;
            this.dgrTable.Rows[idx].Cells["colStatusCode"].Value = item.STATUS_CODE;
            this.dgrTable.Rows[idx].Cells["colStatus"].Value = this.fsGetStatusName(item.STATUS_CODE);
            return true;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmTables.btnDelete_Click", "", "");
            if (this.dgrTable.Rows.Count <= 0)
            {
                return;
            }

            int status = Convert.ToInt32(this.dgrTable.CurrentRow.Cells["colStatusCode"].Value.ToString());
            if (status == (int)TableStatus.BUSY || status == (int)TableStatus.MERGE)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Bàn này đang hoạt động, không được xóa!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }


            DialogResult dlgRs = DevComponents.DotNetBar.MessageBoxEx.Show("Bạn có chắc muốn xóa bàn đang chọn không?", CommonMessages.MSG_CONFIRM_TITLE, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dlgRs != System.Windows.Forms.DialogResult.Yes)
            {
                return;
            }

            TableCtrl tableCtrl = new TableCtrl();
            if (tableCtrl.fsDeleteTable(this.dgrTable.CurrentRow.Cells["colTableCode"].Value.ToString()) != ResultStatus.SUCCESS)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Đã có lổi xảy ra, vui lòng thực hiện lại!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            int idx = this.dgrTable.CurrentRow.Index;
            this.dgrTable.Rows.RemoveAt(idx);
            fsUpdateSTT();
        }

        private void btnReName_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmTables.btnReName_Click", "", "");
            if (this.dgrTable.Rows.Count > 0)
            {
                TableCtrl tableCtrl = new TableCtrl();
                DialogResult dlgRs = DevComponents.DotNetBar.MessageBoxEx.Show("Bạn có chắc muốn thay đổi tên bàn theo số thứ tự tăng dần không?", CommonMessages.MSG_CONFIRM_TITLE, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dlgRs == System.Windows.Forms.DialogResult.Yes)
                {
                    for (int i = 0; i < this.dgrTable.Rows.Count; i++)
                    {
                        this.dgrTable.Rows[i].Cells["colTableName"].Value =
                            this.txtAreaSign.Text + this.dgrTable.Rows[i].Cells["colSTT"].Value.ToString();
                        tableCtrl.fsUpdateTableName(this.dgrTable.Rows[i].Cells["colTableCode"].Value.ToString(),
                            this.dgrTable.Rows[i].Cells["colTableName"].Value.ToString());
                    }
                }
            }
        }

        private void fsUpdateSTT()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmTables.fsUpdateSTT", "", "");
            if (this.dgrTable.Rows.Count == 0)
            {
                return;
            }

            for (int i = 0; i < this.dgrTable.Rows.Count; i++)
            {
                this.dgrTable.Rows[i].Cells["colSTT"].Value = (i + 1).ToString("00");
            }
        }
    }
}
